package com.hmy.uaa.common.security;

import com.hmy.uaa.mapper.UserMapper;
import com.hmy.uaa.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * 根据用户名认证
 */
@Service
public class UserNameDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 用户手机号
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.getUserInfo(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        return new com.hmy.uaa.common.security.User(user.getPhone(), user.getPassword(), Collections.emptyList());
    }


}
